﻿using UnityEngine;
using System.Collections;

/// <summary>  
/// 触碰控制器  
/// </summary>  
public class TouchController : PlatformController
{
    /// <summary>  
    /// 修正比例  
    /// </summary>  
    private float rate = 50f;

    /// <summary>  
    /// 触点一  
    /// </summary>  
    private Touch oneTouch;

    /// <summary>  
    /// 触点二  
    /// </summary>  
    private Touch twoTouch;

    /// <summary>  
    /// 最后一次缩放距离  
    /// </summary>  
    private float lastScaleDistance;

    /// <summary>  
    /// 当前缩放距离  
    /// </summary>  
    private float scaleDistance;

    private Vector2 lastFingerPos;

    private float fingerEndTime;

    void Update()
    {
        if (Input.touchCount > 0)
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch touch = Input.GetTouch(i);
                if (this.multi2OneCallback != null) this.multi2OneCallback(touch.fingerId,i,i,touch.phase);
            }
        }

        //if (Input.touchCount > 0)
        //{
        //    for (int i = 0; i < Input.touchCount; i++)
        //    {
        //        Touch touch = Input.GetTouch(i);
        //        switch (touch.phase)
        //        {
        //            case TouchPhase.Began:
        //                if (this.beginCallback != null) this.beginCallback(touch.position, touch.fingerId);
        //                break;
        //            case TouchPhase.Moved:
        //                break;
        //            case TouchPhase.Stationary:
        //                break;
        //            case TouchPhase.Ended:
        //                if (this.endCallback != null) this.endCallback(touch.position, touch.fingerId);
        //                break;
        //            case TouchPhase.Canceled:
        //                if (this.cancelCallback != null) this.cancelCallback(touch.position, touch.fingerId);
        //                break;
        //            default:
        //                break;
        //        }
        //    }
        //}

        // 如果只有一个触点  
        if (Input.touchCount == 1)
        {
            this.oneTouch = Input.touches[0];
            // 触点开始  
            if (this.oneTouch.phase == TouchPhase.Began)
            {
                // 触发开始回调函数  
                if (this.beginCallback != null) this.beginCallback(GetViewPortPos(oneTouch.position), oneTouch.fingerId);
            }
            // 触点移动  
            else if (oneTouch.phase == TouchPhase.Moved)
            {
                // 触发移动回调函数  
                //if (this.moveCallback != null) this.moveCallback(new Vector2(this.oneTouch.deltaPosition.x, this.oneTouch.deltaPosition.y) / this.rate);
                if (this.moveCallback != null) this.moveCallback(GetViewPortPos(oneTouch.position), oneTouch.fingerId);
            }
            // 触点结束  
            else if (oneTouch.phase == TouchPhase.Ended)
            {
                // 触发结束回调函数  
                Vector2 diff = GetViewPortPos(oneTouch.position) - lastFingerPos;
                if (this.endCallback != null) this.endCallback(diff, oneTouch.fingerId);
                //float x = pos.x / Screen.width;
                //float y = pos.y / Screen.height;
                //Vector2 view = new Vector2(x, y);
                //if (this.endCallback != null) this.endCallback(oneTouch.deltaPosition, oneTouch.fingerId);
            }

            lastFingerPos = GetViewPortPos(oneTouch.position);
        }
        // 如果有多个触点  
        if (Input.touchCount > 1)
        {
            this.oneTouch = Input.touches[0];
            this.twoTouch = Input.touches[1];
            Vector2 oneViewPos = GetViewPortPos(this.oneTouch.position);
            Vector2 twoViewPos = GetViewPortPos(this.twoTouch.position);
            float curDis = Vector2.Distance(oneViewPos, twoViewPos);

            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch touch = Input.GetTouch(i);
                if (touch.phase == TouchPhase.Began)
                {
                    Vector2 pos = GetCenterPos();
                    if (this.beginCallback != null) this.beginCallback(GetViewPortPos(pos));
                    this.lastScaleDistance = curDis;
                    if(Input.touchCount == 2)
                    {
                        if (this.beginScaleCallback != null) this.beginScaleCallback();
                    }
                }
                else if (touch.phase == TouchPhase.Ended)
                {
                    Vector2 pos = GetCenterPos(i);
                    if (this.beginCallback != null) this.beginCallback(GetViewPortPos(pos));
                    this.lastScaleDistance = curDis;
                    if (Input.touchCount == 2)
                    {
                        if (this.endScaleCallback != null) this.endScaleCallback(Time.time);
                    }
                }
            }


            // 如果是缩放  
            if (oneTouch.phase == TouchPhase.Moved || twoTouch.phase == TouchPhase.Moved)
            {                
                //this.scaleDistance = (this.oneTouch.deltaPosition + this.twoTouch.deltaPosition).magnitude;
                //this.scaleDistance = Vector2.Distance(this.oneTouch.position, this.twoTouch.position);
                // 触发缩放回调函数  
                //if (this.scaleCallback != null) this.scaleCallback(this.scaleDistance);
                this.scaleCallback(this.lastScaleDistance - curDis);
                this.lastScaleDistance = curDis;
            }
            

            
        }
        // 触发每帧执行更新  
        if (this.updateCallback != null) this.updateCallback();
    }

    private Vector2 GetCenterPos(int exceptID = -1)
    {
        Vector2 pos = Vector2.zero;
        int continueCount = 0;
        if(Input.touchCount > 0)
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                if(exceptID == i)
                {
                    continueCount++;
                    continue;
                }
                pos += Input.GetTouch(i).position;
            }

            pos /= (Input.touchCount - continueCount);
        }
        return pos;
    }
}